package com.lai.qq.admin.biz.controller;


import com.alibaba.fastjson.JSON;
import com.lai.qq.admin.biz.req.AutoModelReq;
import com.lai.qq.admin.biz.res.RebackInfoResult;
import com.byd.dilink.db.vehicle.api.entity.AutoModel;
import com.byd.dilink.db.vehicle.api.service.AutoModelDbService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequestMapping("/api/admin/autoModel")
@RestController
@Api(tags = "车型模块")
public class AutoModelController {

    @Reference
    private AutoModelDbService autoModelDbService;


    @PostMapping("/selectAutoModel")
    @ApiOperation(value = "根据AutoModelReq查询AutoModelReq")
    public RebackInfoResult selectAutoModel(@RequestBody AutoModelReq req) throws Exception {
        log.info("/selectAutoModel,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        AutoModel autoModel = autoModelDbService.selectOne(param);
        return new RebackInfoResult().success(autoModel);
    }

    @PostMapping("/selectAutoModelList")
    public RebackInfoResult selectAutoModelList(@RequestBody AutoModelReq req) throws Exception {
        log.info("/selectAutoModelList,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        List<AutoModel> list = autoModelDbService.selectListByAutoModel(param);
        return new RebackInfoResult().success(list);
    }

    @PostMapping("/insertAutoModel")
    public RebackInfoResult insertAutoModel(@Valid @RequestBody AutoModelReq req){
        log.info("/insertAutoModel,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        int count =  autoModelDbService.insert(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/updateAutoModel")
    public RebackInfoResult updateAutoModel(@RequestBody @Validated AutoModelReq req, BindingResult bindingResult) throws Exception {
        log.info("/updateAutoModel,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        int count = autoModelDbService.updateByPrimaryKey(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/deleteAutoModel")
    public RebackInfoResult deleteAutoModel(@RequestBody AutoModelReq req) throws Exception {
        log.info("/deleteAutoModel,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        int count = autoModelDbService.delete(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/insertToMongoDb")
    public RebackInfoResult insertToMongoDb(@RequestBody @Validated AutoModelReq req, BindingResult bindingResult) throws Exception {
        log.info("/insertToMongoDb,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        int count =  autoModelDbService.insertToMongoDb(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/updateToMongoDb")
    public RebackInfoResult updateToMongoDb(@RequestBody @Validated AutoModelReq req, BindingResult bindingResult) throws Exception {
        log.info("/updateToMongoDb,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        int count = autoModelDbService.updateToMongoDb(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/deleteToMongoDb")
    public RebackInfoResult deleteToMongoDb(@RequestBody AutoModelReq req) throws Exception {
        log.info("/deleteToMongoDb,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        int count = autoModelDbService.deleteToMongoDb(param);
        if(count!=1){
            return new RebackInfoResult().failure();
        }
        return new RebackInfoResult().success();
    }

    @PostMapping("/selectOneToMongoDb")
    public RebackInfoResult selectToMongoDb(@RequestBody AutoModelReq req) throws Exception {
        log.info("/selectOneToMongoDb,req={}", JSON.toJSONString(req));
        AutoModel param = new AutoModel();
        BeanUtils.copyProperties(req,param);
        AutoModel autoModel = autoModelDbService.selectOneToMongoDb(param);
        return new RebackInfoResult().success(autoModel);
    }

}
